#include "vinointerface.h"
#include <QDebug>
#include <QProcess>

VinoInterface::VinoInterface()
{
    mVinoGsettings = UniversalInterface::self()->gnomeVinoGsettings();

    mGsettingMap[kVinoViewOnlyKey] = QString("viewOnly");
    mGsettingMap[kVinoPromptKey] = QString("promptEnabled");
    mGsettingMap[kAuthenticationKey] = QString("authenticationMethods");
    mGsettingMap[kVncPwdKey] = QString("vncPassword");
}

bool VinoInterface::getViewOnly()
{
    if (mVinoGsettings->keys().contains("viewOnly")) {
        return mVinoGsettings->get(kVinoViewOnlyKey).toBool();
    } else {
        qCritical() << "mVinoGsettings not contains the key: " << kVinoViewOnlyKey;
    }
    return false;
}

bool VinoInterface::getPromptEnabled()
{
    if (mVinoGsettings->keys().contains("promptEnabled")) {
        return mVinoGsettings->get(kVinoPromptKey).toBool();
    } else {
        qCritical() << "mVinoGsettings not contains the key: " << kVinoPromptKey;
    }
    return false;
}

QString VinoInterface::getMethod()
{
    if (mVinoGsettings->keys().contains("authenticationMethods")) {
        return mVinoGsettings->get(kAuthenticationKey).toString();
    } else {
        qCritical() << "mVinoGsettings not contains the key: " << kAuthenticationKey;
    }
    return QString();
}

QString VinoInterface::getPassword()
{
    if (mVinoGsettings->keys().contains("vncPassword")) {
        return mVinoGsettings->get(kVncPwdKey).toString();
    } else {
        qCritical() << "mVinoGsettings not contains the key: " << kVncPwdKey;
    }
    return QString();
}

bool VinoInterface::getVinoServerStatus()
{
    QProcess process;
    process.start("systemctl", QStringList() << "--user" << "is-active" << "vino-server.service");
    process.waitForFinished();
    bool isActive = process.readAllStandardOutput().replace("\n", "") == "active";
    return isActive;

}

void VinoInterface::setVinoKey(const QString &key, bool value)
{
    if (mVinoGsettings->keys().contains(mGsettingMap[key])) {
        return mVinoGsettings->set(key, value);
    } else {
        qCritical() << "ukuiNoticeGsettings not contains the key: " << key;
    }
}

void VinoInterface::setVinoKey(const QString &key, QString value)
{
    if (mVinoGsettings->keys().contains(mGsettingMap[key])) {
        return mVinoGsettings->set(key, value);
    } else {
        qCritical() << "ukuiNoticeGsettings not contains the key: " << key;
    }
}

void VinoInterface::setVinoKey(const QString &key, QByteArray value)
{
    if (mVinoGsettings->keys().contains(mGsettingMap[key])) {
        return mVinoGsettings->set(key, value);
    } else {
        qCritical() << "ukuiNoticeGsettings not contains the key: " << key;
    }
}
